<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tomcat虚拟主机配置指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6B73FF 0%, #000DFF 100%);
        }
        .code-block {
            background-color: #f8f9fa;
            border-left: 4px solid #4f46e5;
            color: #374151;
        }
        .step-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .note-box {
            border-left: 4px solid #10B981;
            background-color: #ECFDF5;
        }
        .warning-box {
            border-left: 4px solid #F59E0B;
            background-color: #FFFBEB;
        }
        h1, h2, h3 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 max-w-4xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 tracking-tight">Tomcat虚拟主机配置指南</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl">一站式掌握Tomcat虚拟主机配置技巧，实现多站点托管</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm font-medium">服务器配置</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm font-medium">网络托管</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm font-medium">Tomcat优化</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-4xl">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 leading-relaxed mb-8">
                在Tomcat中配置虚拟主机(Virtual Host)可以让你在同一个Tomcat实例上托管多个网站或应用，每个网站或应用具有不同的域名或子域名。这不仅能有效利用服务器资源，还能简化管理流程。本文将详细讲解如何配置Tomcat虚拟主机。
            </p>
            
            <!-- Diagram -->
            <div class="bg-white rounded-xl shadow-md p-6 mb-12">
                <div class="mermaid">
                    graph LR
                    A[Tomcat服务器] --> B[虚拟主机1: demo.demo.com]
                    A --> C[虚拟主机2: api.demo.com]
                    A --> D[虚拟主机3: blog.demo.com]
                    B --> E[webapps/demo]
                    C --> F[webapps/api]
                    D --> G[webapps/blog]
                </div>
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-4 flex items-center">
                <i class="fas fa-file-code mr-3 text-blue-600"></i>
                1. 配置server.xml文件
            </h2>
            
            <p class="mb-6 text-gray-700">
                在Tomcat的<code class="bg-gray-100 px-2 py-1 rounded">conf</code>目录下，有一个主要的配置文件<code class="bg-gray-100 px-2 py-1 rounded">server.xml</code>。要配置虚拟主机，需要在该文件中添加<code class="bg-gray-100 px-2 py-1 rounded">&lt;Host&gt;</code>元素。
            </p>
            
            <h3 class="text-2xl font-semibold mt-8 mb-4 text-gray-700 flex items-center">
                <i class="fas fa-cog mr-2 text-blue-500"></i>
                1.1 编辑server.xml文件
            </h3>
            
            <p class="mb-4 text-gray-700">
                找到<code class="bg-gray-100 px-2 py-1 rounded">server.xml</code>文件中的<code class="bg-gray-100 px-2 py-1 rounded">&lt;Engine&gt;</code>元素，通常位于文件的中间部分。你需要在<code class="bg-gray-100 px-2 py-1 rounded">&lt;Engine&gt;</code>元素内添加<code class="bg-gray-100 px-2 py-1 rounded">&lt;Host&gt;</code>元素来定义虚拟主机。
            </p>
            
            <div class="code-block p-4 rounded-lg my-6 overflow-x-auto">
                <pre><code class="language-xml">
&lt;Engine name="Catalina" defaultHost="localhost"&gt;
    &lt;!-- 默认的主机配置 --&gt;
    &lt;Host name="localhost"  appBase="webapps"
          unpackWARs="true" autoDeploy="true"&gt;
        &lt;Context path="" docBase="${catalina.home}/webapps/ROOT" /&gt;
    &lt;/Host&gt;

    &lt;!-- 自定义虚拟主机 --&gt;
    &lt;Host name="demo.demo.com" appBase="webapps/demo"
          unpackWARs="true" autoDeploy="true"&gt;
        &lt;Alias&gt;www.demo.demo.com&lt;/Alias&gt;

        &lt;Context path="" docBase="${catalina.home}/webapps/demo" /&gt;
    &lt;/Host&gt;

    &lt;Host name="api.demo.com" appBase="webapps/api"
          unpackWARs="true" autoDeploy="true"&gt;
        &lt;Context path="" docBase="${catalina.home}/webapps/api" /&gt;
    &lt;/Host&gt;
&lt;/Engine&gt;
                </code></pre>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-8">
                <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100 step-card transition duration-300">
                    <div class="flex items-start mb-4">
                        <div class="bg-blue-100 p-2 rounded-full mr-4">
                            <i class="fas fa-tag text-blue-600"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800">name</h4>
                            <p class="text-gray-600">定义了虚拟主机的域名。例如<code class="text-sm bg-gray-100 px-1">demo.demo.com</code>和<code class="text-sm bg-gray-100 px-1">api.demo.com</code></p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100 step-card transition duration-300">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-4">
                            <i class="fas fa-folder-open text-purple-600"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800">appBase</h4>
                            <p class="text-gray-600">指定了虚拟主机的应用根目录。可以是Tomcat的<code class="text-sm bg-gray-100 px-1">webapps</code>目录下的子目录</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100 step-card transition duration-300">
                    <div class="flex items-start mb-4">
                        <div class="bg-green-100 p-2 rounded-full mr-4">
                            <i class="fas fa-box-open text-green-600"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800">unpackWARs</h4>
                            <p class="text-gray-600">设置是否解压WAR文件</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100 step-card transition duration-300">
                    <div class="flex items-start mb-4">
                        <div class="bg-yellow-100 p-2 rounded-full mr-4">
                            <i class="fas fa-rocket text-yellow-600"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800">autoDeploy</h4>
                            <p class="text-gray-600">设置是否自动部署应用</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="note-box p-4 rounded-lg my-6">
                <div class="flex items-start">
                    <i class="fas fa-info-circle text-green-500 mt-1 mr-3"></i>
                    <div>
                        <h4 class="font-bold text-gray-800 mb-2">Alias标签</h4>
                        <p class="text-gray-700">可以指定虚拟主机的别名，例如<code class="text-sm bg-green-100 px-1">www.demo.demo.com</code>。这样用户访问www.demo.demo.com和demo.demo.com都会被路由到同一个虚拟主机。</p>
                    </div>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mt-12 mb-4 text-gray-700 flex items-center">
                <i class="fas fa-sync-alt mr-2 text-blue-500"></i>
                1.2 重新加载配置
            </h3>
            
            <p class="mb-4 text-gray-700">
                修改<code class="bg-gray-100 px-2 py-1 rounded">server.xml</code>文件后，需要重新加载Tomcat的配置才能使更改生效。可以通过重启Tomcat实例来实现：
            </p>
            
            <div class="code-block p-4 rounded-lg my-6">
                <pre><code class="language-bash">
# 重启Tomcat
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh
                </code></pre>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-4 flex items-center">
                <i class="fas fa-cubes mr-3 text-blue-600"></i>
                2. 创建和部署应用
            </h2>
            
            <p class="mb-6 text-gray-700">
                在上面的配置示例中，我们将应用程序放置在Tomcat的<code class="bg-gray-100 px-2 py-1 rounded">webapps</code>目录下的相应子目录中。例如，<code class="bg-gray-100 px-2 py-1 rounded">webapps/demo</code>目录下的应用将被映射到<code class="bg-gray-100 px-2 py-1 rounded">demo.demo.com</code>域名。
            </p>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 my-8">
                <div class="bg-white p-5 rounded-lg shadow-sm border border-gray-100">
                    <div class="flex items-center mb-3">
                        <div class="bg-blue-500 text-white p-2 rounded-full mr-3">
                            <i class="fas fa-folder"></i>
                        </div>
                        <h4 class="font-bold text-gray-800">webapps/demo</h4>
                    </div>
                    <p class="text-gray-600">放置demo虚拟主机的应用</p>
                </div>
                
                <div class="bg-white p-5 rounded-lg shadow-sm border border-gray-100">
                    <div class="flex items-center mb-3">
                        <div class="bg-purple-500 text-white p-2 rounded-full mr-3">
                            <i class="fas fa-folder"></i>
                        </div>
                        <h4 class="font-bold text-gray-800">webapps/api</h4>
                    </div>
                    <p class="text-gray-600">放置api虚拟主机的应用</p>
                </div>
            </div>
            
            <div class="note-box p-4 rounded-lg my-6">
                <div class="flex items-start">
                    <i class="fas fa-lightbulb text-green-500 mt-1 mr-3"></i>
                    <div>
                        <h4 class="font-bold text-gray-800 mb-2">部署建议</h4>
                        <p class="text-gray-700">确保你的应用程序以合适的目录结构部署在这些位置。对于WAR包部署，Tomcat会自动解压到相应的目录中。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-4 flex items-center">
                <i class="fas fa-globe mr-3 text-blue-600"></i>
                3. DNS配置
            </h2>
            
            <p class="mb-6 text-gray-700">
                为了使虚拟主机能够正常工作，你需要确保DNS配置正确，将虚拟主机的域名指向Tomcat服务器的IP地址。这通常涉及到在DNS服务提供商处添加适当的A记录或CNAME记录。
            </p>
            
            <div class="bg-white rounded-xl shadow-md p-6 my-8">
                <div class="mermaid">
                    sequenceDiagram
                        participant 用户
                        participant DNS服务器
                        participant Tomcat服务器
                        用户->>DNS服务器: 查询demo.demo.com的IP
                        DNS服务器-->>用户: 返回Tomcat服务器IP
                        用户->>Tomcat服务器: 访问demo.demo.com
                        Tomcat服务器-->>用户: 返回webapps/demo内容
                </div>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-4 flex items-center">
                <i class="fas fa-file-alt mr-3 text-blue-600"></i>
                4. 虚拟主机的日志
            </h2>
            
            <p class="mb-6 text-gray-700">
                每个虚拟主机可以配置自己的日志文件。这可以通过在<code class="bg-gray-100 px-2 py-1 rounded">&lt;Host&gt;</code>元素中添加<code class="bg-gray-100 px-2 py-1 rounded">AccessLogValve</code>和<code class="bg-gray-100 px-2 py-1 rounded">Engine</code>元素来实现。
            </p>
            
            <div class="code-block p-4 rounded-lg my-6">
                <pre><code class="language-xml">
&lt;Host name="demo.demo.com" appBase="webapps/demo"
      unpackWARs="true" autoDeploy="true"&gt;
    &lt;Alias&gt;www.demo.demo.com&lt;/Alias&gt;

    &lt;Context path="" docBase="${catalina.home}/webapps/demo" /&gt;
    
    &lt;Valve className="org.apache.catalina.valves.AccessLogValve"
           directory="logs/demo"
           prefix="localhost_access_log"
           suffix=".txt"
           pattern="common"/&gt;
&lt;/Host&gt;
                </code></pre>
            </div>
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-8">
                <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100">
                    <div class="flex items-start mb-4">
                        <div class="bg-blue-100 p-2 rounded-full mr-4">
                            <i class="fas fa-folder text-blue-600"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800">directory</h4>
                            <p class="text-gray-600">日志文件存储目录，相对于<code class="text-sm bg-gray-100 px-1">$CATALINA_BASE</code></p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100">
                    <div class="flex items-start mb-4">
                        <div class="bg-purple-100 p-2 rounded-full mr-4">
                            <i class="fas fa-file text-purple-600"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-gray-800">prefix/suffix</h4>
                            <p class="text-gray-600">日志文件的前缀和后缀，用于生成文件名</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 5 -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-4 flex items-center">
                <i class="fas fa-exclamation-triangle mr-3 text-blue-600"></i>
                5. 注意事项
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="warning-box p-5 rounded-lg">
                    <div class="flex items-start">
                        <i class="fas fa-exclamation-circle text-yellow-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-bold text-gray-800 mb-2">冲突检查</h4>
                            <p class="text-gray-700">确保不同虚拟主机的<code class="text-sm bg-yellow-100 px-1">appBase</code>目录不同，以避免应用冲突。</p>
                        </div>
                    </div>
                </div>
                
                <div class="warning-box p-5 rounded-lg">
                    <div class="flex items-start">
                        <i class="fas fa-memory text-yellow-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-bold text-gray-800 mb-2">内存和资源</h4>
                            <p class="text-gray-700">虚拟主机的数量增加可能会影响Tomcat的内存和性能。监控并调整Tomcat的内存设置以适应负载。</p>
                        </div>
                    </div>
                </div>
                
                <div class="warning-box p-5 rounded-lg">
                    <div class="flex items-start">
                        <i class="fas fa-shield-alt text-yellow-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-bold text-gray-800 mb-2">安全性</h4>
                            <p class="text-gray-700">通过设置适当的访问控制和安全配置来保护虚拟主机及其应用。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="bg-blue-50 rounded-xl p-8 mb-16">
            <h3 class="text-2xl font-bold mb-6 text-blue-800 flex items-center">
                <i class="fas fa-check-circle mr-3 text-blue-600"></i>
                配置总结
            </h3>
            
            <ol class="list-decimal pl-6 space-y-4 text-blue-900">
                <li>编辑<code class="bg-blue-100 px-1.5 py-0.5 rounded">server.xml</code>，添加<code class="bg-blue-100 px-1.5 py-0.5 rounded">&lt;Host&gt;</code>元素</li>
                <li>为每个虚拟主机创建相应的应用目录</li>
                <li>配置DNS记录指向服务器IP</li>
                <li>考虑设置独立的日志文件</li>
                <li>监控性能和资源使用情况</li>
            </ol>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto px-6 max-w-4xl">
            <div class="flex flex-col items-center text-center">
                <div class="text-2xl font-bold text-white mb-4">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-200">
                    <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                </a>
                <div class="mt-6 flex space-x-4">
                    <a href="#" class="text-gray-400 hover:text-white transition duration-200">
                        <i class="fab fa-github"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-200">
                        <i class="fab fa-twitter"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-white transition duration-200">
                        <i class="fab fa-weixin"></i>
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>